package com.zhp.fs.utils;

import java.math.BigDecimal;

/**
 * 时间：2017/5/31  15:36
 * 作者 ：傅文江
 */
public class StringUtils {
    /**
     * 单个数字1-9 转化为简体中文
     *
     * @param number int类型数字
     * @return 简体中文汉字
     */
    public static String int2HanZi(int number) {
        if (number < 1 || number > 9) {
            return "未知";
        }
        String[] strList = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        return strList[number - 1];
    }

    public static boolean isNullOrEmpty(String str) {
        return null == str || "".equals(str.trim());
    }

    /**
     * 0.14213 转 14.21%
     */
    public static String float2PercentStr(Float num) {
        num = num * 100;
        BigDecimal bd = new BigDecimal(num);
        bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
        return bd.toString() + "%";
    }
    /**
     * 0.14213 转 14.21%
     */
    public static String float2PercentStr(Float num,int count) {
        num = num * 100;
        BigDecimal bd = new BigDecimal(num);
        bd = bd.setScale(count, BigDecimal.ROUND_HALF_UP);
        return bd.toString() + "%";
    }

    /**
     * 将float类型小数以保留count位
     *
     * @param f     进入的小数
     * @param count 需要保留的位数
     * @return 结果
     */
    public static String floatWithCount(float f, int count) {
        BigDecimal bd = new BigDecimal(f);
        bd = bd.setScale(count, BigDecimal.ROUND_HALF_UP);
        return bd.toString();
    }


    /**
     * 10000 转  2分钟30秒
     *
     * @param time
     * @return
     */
    public static String time2Str(long time) {
        long mSec = time % 1000;
        time /= 1000;
        long year = time / (365 * 24 * 3600);
        time = time % (365 * 24 * 3600);
        long month = time / (30 * 24 * 3600);
        time = time % (30 * 24 * 3600);
        long day = time / (24 * 3600);
        time = time % (24 * 3600);
        long hour = time / 3600;
        time = time % 3600;
        long min = time / 60;
        time = time % 60;
        long sec = time;
        StringBuilder sb = new StringBuilder();

        if (0 != year) {
            sb.append(year).append("年");
        }
        if (0 != month) {
            sb.append(month).append("月");
        }
        if (0 != day) {
            sb.append(day).append("天");
        }
        if (0 != hour) {
            sb.append(hour).append("小时");
        }
        if (0 != min) {
            sb.append(min).append("分钟");
        }
        if (0 != sec) {
            sb.append(sec).append("秒");
        }
        /*if (0 != mSec) {
            sb.append(year).append("毫秒");
        }*/

        return sb.toString();
    }

    /**
     * 10000 转  2:30
     *
     * @param time 毫秒
     * @return
     */
    public static String time2StrWithF(long time) {
        time = time / 1000;
        long min = time / 60;
        time = time % 60;
        long sec = time;
        if (sec < 10) {
            return min + ":0" + sec;
        } else {
            return min + ":" + sec;
        }
    }

    /**
     * 对float类型的数字保留固定小数位
     *
     * @param scale 需要保留的小数位
     * @param ft    需要格式化的小数
     * @return 结果
     */
    public static float floatWithscale(float ft, int scale) {
        int roundingMode = 4;//表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
        BigDecimal bd = new BigDecimal((double) ft);
        bd = bd.setScale(scale, roundingMode);
        return bd.floatValue();
    }
}
